<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpt_menu.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');


class TCMS_VPT_Menu_DBC extends TCMS_VPT_menu implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select template, menu from tcms_vpt_menus where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_template($rez_obj->TEMPLATE);
			$this->set_menu(stripslashes($rez_obj->menu));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpt_menus_seq');
		$template = $this->get_template();
		$menu = $this->get_menu();

		if ($id) {
			$sql = "insert into tcms_vpt_menus (id, ".
									      "template, ".
									      "menu ".
									      ") ".
								"values ".
									      "($id,".
									      "$template, ".
									      "$menu ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpt_menus_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$template = $this->get_template();
		$menu = $this->get_menu();

		$sql = "update tcms_vpt_menus set ".
						"template = $template, ".
						"menu = $menu ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpt_menus where id = $id";
		$dbc->execute($sql);
	}


	public static function select_all_for_template(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);

		$sql = "select vptm.id, m.name from tcms_vpt_menus vptm, tcms_menus m where vptm.template = $template_id and vptm.menu = m.id";

		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = array('id' => $rez_obj->ID, 'menu_name' => $rez_obj->NAME);
		}

		return $ret;
	}


	public static function exists(DB_Connection $dbc, $template_id, $menu_id) {
		tangra_if_not_int_throw_e($template_id);
		tangra_if_not_int_throw_e($menu_id);

		$sql = "select id from tcms_vpt_menus where template = $template_id and menu = $menu_id";
		$rez = $dbc->execute($sql);

		return !$rez->is_eod();
	}


	public static function load_all_for_template(DB_Connection $dbc, $template_id) {
		tangra_if_not_int_throw_e($template_id);

		$sql = "select menu from tcms_vpt_menus where template = $template_id";
		$rez = $dbc->execute($sql);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_Menu_DBC();
			$tmp->load_by_id($dbc, $rez_obj->MENU);

			$ret[] = clone $tmp;
		}

		return $ret;
	}


	public static function count_times_menu_used(DB_Connection $dbc, $menu_id) {
		tangra_if_not_int_throw_e($menu_id);

		$sql = "select count(id) as C from tcms_vpt_menus where menu = $menu_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public static function count_times_menu_type_used(DB_Connection $dbc, $menu_type_id) {
		tangra_if_not_int_throw_e($menu_type_id);

		$sql = "select count(vptm.id) as C from tcms_vpt_menus vptm, tcms_menus m where m.type = $menu_type_id and vptm.menu = m.id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public static function delete_for_menu(DB_Connection $dbc, $menu_id) {
		tangra_if_not_int_throw_e($menu_id);

		$sql = "delete from tcms_vpt_menus where menu = $menu_id";
		$dbc->execute($sql);
	}
}